//
//  SRDataView.m
//  SportRing
//
//  Created by elwin on 10/30/14.
//  Copyright (c) 2014 HotOne. All rights reserved.
//

#import "SRDataView.h"
#import "HKCircleChartView.h"
#import "PNChart.h"
#import "UIScrollView+SVPullToRefresh.h"
#import "SRDay+Cate.h"

@implementation SRDataViewModels


@end

@implementation SRDataViewModel

@end

@interface SRDataView()<HKCircleChartViewDataSoucre,PNChartDelegate>
{
    __block NSMutableArray *yDataArray;
    
    __block NSMutableArray *xDataArray;
    
    __block NSMutableArray *realXdataArray;

}

@property (nonatomic, weak)IBOutlet UIImageView* distanceChartContainerView;
@property (nonatomic, weak)IBOutlet UILabel* distanceLabel;
@property (nonatomic, weak)IBOutlet UILabel* stepsLabel;
@property (nonatomic, weak)IBOutlet UILabel* kalorieLabel;
@property (nonatomic, weak)IBOutlet UILabel* PNChartTitleLabel;
@property (nonatomic, weak)IBOutlet UIScrollView* monthSportDataContainerScrollView;

@property (nonatomic, weak)IBOutlet UIScrollView*containnerScrollview;

@property (nonatomic, strong)PNLineChartData *distanceData;

@property (nonatomic, strong)PNLineChart * lineChart;

@end

@implementation SRDataView


- (void)awakeFromNib
{
    [super awakeFromNib];
        
    [self.containnerScrollview setContentSize:CGSizeMake(self.containnerScrollview.frame.size.width, self.containnerScrollview.frame.size.height )];
    
    //去掉下拉刷新
    self.containnerScrollview.scrollEnabled = NO;
    
    [self.containnerScrollview addPullToRefreshWithActionHandler:^{
        
        if (_dataDelegate && [_dataDelegate respondsToSelector:@selector(updateData)])
        {
            [_dataDelegate updateData];
        }
    }];
}

- (void)refreshData
{
    [_containnerScrollview triggerPullToRefresh];
}

- (void)stopAnimating
{
    [_containnerScrollview.pullToRefreshView stopAnimating];
}

- (void)setSRDataViewData:(SRDataViewModels *)models total:(SRDataViewModel *)model
{
    [self addLineChartWithArray:models];
    [self setKaloriChartViewWithProcess:model.process];
    self.distanceLabel.text = [NSString stringWithFormat:@"%.1f",model.distance];
    self.stepsLabel.text = [NSString stringWithFormat:@"%.f",model.steps];
    self.kalorieLabel.text = [NSString stringWithFormat:@"%d",(int)model.kalorie];
    self.PNChartTitleLabel.text = [NSString stringWithFormat:@"%@",[NSDate stringFromDate:model.creatDate format:@"yyyy年MM月"]];
    
}

- (void)setKaloriChartViewWithProcess:(float)process
{
    for (UIView *view in self.distanceChartContainerView.subviews) {
        if ([view isKindOfClass:[HKCircleChartView class]]) {
            [view removeFromSuperview];
        }
    }
    HKCircleChartView *chartView = [[HKCircleChartView alloc] initWithFrame:CGRectMake(0, 0, self.distanceChartContainerView.frame.size.width, self.distanceChartContainerView.frame.size.height)];
    
    chartView.dataSoucre = self;
    
    [self.distanceChartContainerView addSubview:chartView];
    
    [chartView makeCircleChartProcess:process animation:YES];
    
}

- (void)addLineChartWithArray:(SRDataViewModels *)models
{
    [self.lineChart removeFromSuperview];
    
    //每一页显示的point个数
    int numberPerPage = 7;
    
    //页数
    NSInteger x_count;
    
    self.PNChartTitleLabel.textColor = [UIColor colorWithRed:104.0/255.0 green:209.0/255.0 blue:252.0/255.0 alpha:1.0f];
    self.PNChartTitleLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:14.0];
    
    
    if (models.dataArray && [models.dataArray count] > 7)
    {
        int page = [models.dataArray count]%numberPerPage;
        
        if (page == 0)
        {
            x_count = [models.dataArray count]/numberPerPage;
        }
        else
        {
            x_count = [models.dataArray count]/numberPerPage + 1;
        }
    }
    else
    {
        x_count = 1;
    }

    self.monthSportDataContainerScrollView.contentSize = CGSizeMake(self.monthSportDataContainerScrollView.frame.size.width*x_count, self.monthSportDataContainerScrollView.frame.size.height);
    
    //x,y:data
    xDataArray = [[NSMutableArray alloc] init];
    yDataArray = [[NSMutableArray alloc] init];
    realXdataArray = [[NSMutableArray alloc] init];
    self.lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 0, self.monthSportDataContainerScrollView.contentSize.width, self.monthSportDataContainerScrollView.frame.size.height-10)];
    
    [models.dataArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
    {
        SRDataViewModel *model = (SRDataViewModel *)obj;
        
        if (model.creatDate)
        {
            [xDataArray addObject:[NSDate stringFromDate:model.creatDate format:@"M.d"]];
            [realXdataArray addObject:model.creatDate];
        }
        else{
            [xDataArray addObject:[NSDate stringFromDate:[NSDate date] format:@"M.d"]];
            [realXdataArray addObject:[NSDate date]];
        }
        [yDataArray addObject:[NSString stringWithFormat:@"%.1f",model.steps]];

    }];
    
    self.lineChart.showLabel = YES;
    self.lineChart.chartMargin = 20;

    self.lineChart.yLabelColor = [UIColor clearColor];

    self.lineChart.yLabelFont = [UIFont systemFontOfSize:10];
    self.lineChart.backgroundColor = [UIColor clearColor];
    self.lineChart.xLabelColor = [UIColor colorWithRed:104.0/255.0 green:209.0/255.0 blue:252.0/255.0 alpha:1.0f];
    self.lineChart.xLabelFont = [UIFont systemFontOfSize:10];
    [self.lineChart setXLabels:xDataArray];
    self.lineChart.showCoordinateAxis = NO;
    
    self.distanceData = [PNLineChartData new];
    self.distanceData.color = [UIColor colorWithRed:65.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0f];
    self.distanceData.itemCount = self.lineChart.xLabels.count;
    self.distanceData.inflexionPointStyle = PNLineChartPointStyleCircle;
    
    __weak NSMutableArray *data = yDataArray;
    self.distanceData.getData = ^(NSUInteger index) {
        CGFloat yValue = [data[index] floatValue];
        return [PNLineChartDataItem dataItemWithY:yValue];
    };

    self.lineChart.chartData = @[self.distanceData];
    
    [self.lineChart strokeChart];
    
    self.lineChart.delegate = self;

    [self.monthSportDataContainerScrollView addSubview:self.lineChart];
}

#pragma mark - HKCircleChartViewDataSoucre

- (HKCircleChartSettingModel *)circleChartViewSettingModel
{
    
    HKCircleChartSettingModel *model = [[HKCircleChartSettingModel alloc] init];
    
    model.clockwise = YES;
    
    model.ringWidth = 8;
    
    model.animationDuration = 0.5;
    
    model.circleColer = [UIColor colorWithRed:127.0/255.0 green:208.0/255.0 blue:28.0/255.0 alpha:1.0f];
    
    model.circleBGColer = [UIColor colorWithRed:249.0/255.0 green:248.0/255.0 blue:242.0/255.0 alpha:1.0f];
    
    model.circleBGColer = [UIColor clearColor];
    
    return model;
    
}

#pragma mark - PNChartDelegate

- (void)userClickedOnLinePoint:(CGPoint )point lineIndex:(NSInteger)lineIndex
{
    NSLog(@"");
    
}

/**
 * When user click on the chart line key point
 *
 */
- (void)userClickedOnLineKeyPoint:(CGPoint )point lineIndex:(NSInteger)lineIndex andPointIndex:(NSInteger)pointIndex PNChartView:(PNLineChart *)line
{
    if (_dataDelegate && [_dataDelegate respondsToSelector:@selector(clickedOnLineKeyPoint:lineIndex:andPointDate:)])
    {
        [_dataDelegate clickedOnLineKeyPoint:point lineIndex:lineIndex andPointDate:[realXdataArray objectAtIndex:pointIndex]];
        
    }
}

- (void)userClickedOnBarCharIndex:(NSInteger)barIndex
{
}


@end
